<?php
//http://example.com/[controller-class]/[controller-method]/[arguments]

defined('BASEPATH') OR exit('No direct script access allowed');

/*
CREATE TABLE vexation (
    vid INTEGER PRIMARY KEY AUTOINCREMENT,
    owner INTEGER NOT NULL,
	belong INTEGER NOT NULL,
    summary varchar(255),
    detail varchar(2550),
    u_attach_entry int(11), 
    solution varchar(2550),
    v_attach_entry int(11)
);
*/

class Vexation extends CI_Controller {

	public function solve()
	{
		$this->load->view('vex_solve');
	}
	
	public function attach()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->library('vexuser');
		
		if (($userinfo = $this->vexuser->is_logged_in()) === False) {
			$data['notice'] = 'You haven\'t logged in.<br /><a href="../user/login">Login</a>';
			$this->load->view('users/login_notice', $data);
			return;
		}
		
		$this->form_validation->set_rules('summary', 'Summary', 'required');
		$this->form_validation->set_rules('detail', 'Detail', 'required');
		
		if ($this->form_validation->run() === FALSE) {
			# $this->load->view('templates/header');
			$this->load->view('vexations/vex_attach');
			# $this->load->view('templates/footer');
		} else {
			$this->load->model('vex_model');
			$vexinfo = array('owner' => $userinfo,
							 'belong' => 0,
							 'summary' => $this->input->post("summary"),
							 'detail' => $this->input->post("detail")
							 );
			$this->vex_model->write_new_vex($vexinfo);
			$data['notice'] = 'YOU HAVE SUBMITTED!!!!';
			$this->load->view('vexations/vex_attach', $data);
			
		}
		
	}
	
	public function show()
	{
		$this->load->library('vexuser');
		if (($userinfo = $this->vexuser->is_logged_in()) === False) {
			$data['notice'] = 'You haven\'t logged in.<br /><a href="../user/login">Login</a>';
			$this->load->view('users/login_notice', $data);
			return;
		}
		
		$this->load->model('vex_model');
		# TODO: Fix this vulnerable stuff

		if (!isset($_GET['vid'])) {
			//Thus we can show the vexation list
			$query = $this->vex_model->get_vex_list();
			$data['query'] = $query;
			$this->load->view('vexations/vex_show_list', $data);
		} else {
			$query = $this->vex_model->get_vex_info($_GET['vid']);
			$data['query'] = $query;
			$this->load->view('vexations/vex_show', $data);
		}
	}
	
}
